﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Windows.Forms;
using Cluster.LiveJournalLib;

namespace Cluster.JeJePoster
{
    public partial class PasswordForm : Form
    {
        readonly LiveJournalClient ljClient;
        readonly ResourceManager resources = new ResourceManager("JeJePoster.Languages", Assembly.GetExecutingAssembly());
        public PasswordForm(LiveJournalClient liveJournalClient)
        {
            InitializeComponent();
            ljClient = liveJournalClient;
        }

        private void menuItemCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menuItemOk_Click(object sender, EventArgs e)
        {
            ljClient.Password = textBoxPassword.Text;
            var pstoForm = new PstoForm(ljClient, null);
            var pleaseWaitForm = new PleaseWaitForm();
            pleaseWaitForm.Show();
            pleaseWaitForm.BringToFront();
            Hide();
            Cursor.Current = Cursors.WaitCursor;
            Application.DoEvents();
            try
            {
                ljClient.Login(false, false, false, false);
                if (checkBoxSavePassword.Checked) ljClient.SavePassword();
                pstoForm.Show();
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,
                                resources.GetString("Error"), MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation,
                                MessageBoxDefaultButton.Button1);
            }
            finally
            {
                Show();
                pleaseWaitForm.Close();
                Cursor.Current = Cursors.Default;
            }
        }
    }
}